package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.constant.DeleteEnum;
import com.lst.api.entity.PersonInfo;
import com.lst.api.exception.RequestParamErrorException;
import com.lst.api.mapper.PersonInfoMapper;
import com.lst.api.service.IPersonInfoService;
import com.lst.api.vo.PersonInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 借款人个人信息表业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class PersonInfoService extends BaseService<PersonInfoMapper, PersonInfo> implements IPersonInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, PersonInfoVo personInfoVo) {
        if (personInfoVo == null) {
            return false;
        }
        personInfoVo.check();
        PersonInfo originPersonInfo = this.getByIdCardNo(personInfoVo.getIdCardNo());
        if (originPersonInfo != null && !applyNo.equals(originPersonInfo.getApplyNo())) {
            throw new RequestParamErrorException("填写的身份证号已存在");
        }
        PersonInfo personInfo = new PersonInfo(userId, applyNo, personInfoVo);
        QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(PersonInfo.APPLY_NO, applyNo)
                .eq(PersonInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(personInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<PersonInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(PersonInfo.APPLY_NO, applyNo)
                    .eq(PersonInfo.PROMO_USER_ID, userId)
                    .set(PersonInfo.UPDATE_AT, new Date())
                    .set(PersonInfo.NAME, personInfo.getName())
                    .set(PersonInfo.NATIVE_PROVINCE_CODE, personInfo.getNativeProvinceCode())
                    .set(PersonInfo.NATIVE_CITY_CODE, personInfo.getNativeCityCode())
                    .set(PersonInfo.MOBILE, personInfo.getMobile())
                    .set(PersonInfo.MARITAL_STATUS, personInfo.getMaritalStatus())
                    .set(PersonInfo.ID_CARD_NO, personInfo.getIdCardNo())
                    .set(PersonInfo.RESIDE_ADDRESS_PROVINCE_CODE, personInfo.getResideAddressProvinceCode())
                    .set(PersonInfo.RESIDE_ADDRESS_CITY_CODE, personInfo.getResideAddressCityCode())
                    .set(PersonInfo.RESIDE_ADDRESS_AREA_CODE, personInfo.getResideAddressAreaCode())
                    .set(PersonInfo.RESIDE_ADDRESS_DETAILS, personInfo.getResideAddressDetails())
                    .set(PersonInfo.RESIDE_TYPE_ID, personInfo.getResideTypeId());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public boolean idCardNoIsExist(String idCardNo) {
        QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(PersonInfo.ID_CARD_NO, idCardNo);
        return super.getOne(queryWrapper) != null;
    }

    @Override
    public PersonInfo getByIdCardNo(String idCardNo) {
        QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(PersonInfo.ID_CARD_NO, idCardNo)
                .eq(PersonInfo.IS_DELETE, DeleteEnum.DELETE_NO.getKey());
        return super.getOne(queryWrapper);
    }
    
    
    @Override
    public PersonInfo getOneByApplyNo(String applyNo) {
    	QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq(PersonInfo.APPLY_NO, applyNo)
    			.eq(PersonInfo.IS_DELETE, DeleteEnum.DELETE_NO.getKey());
        return super.getOne(queryWrapper);
    }
    
}